import axios from "axios";
import { apiURL } from "../baseURL/baseURL";
import { Toast } from "antd-mobile";
// axios.defaults.withCredentials = true;//登录后发送的请求会自动携带cookie
// 引入组件Toast
// import { Toast } from "antd-mobile";

const instance = axios.create({
  baseURL: apiURL,
});

// Add a request interceptor
instance.interceptors.request.use(
  function (config) {
    // 获取token
    let token = localStorage.getItem("token");
    // token存在，放在请求头中
    if (token) {
      config.headers.Authorization = token;
    }
    Toast.loading("正在加载中...", 0, null, true);
    return config;
  },
  function (error) {
    Toast.hide();
    return Promise.reject(error);
  }
);

// Add a response interceptor
instance.interceptors.response.use(
  function (response) {
    Toast.hide();
    return response.data;
  },
  function (error) {
    Toast.hide();
    return Promise.reject(error);
  }
);

export default instance;
